<template>
  <div>
    <div style="display: flex; justify-content: flex-end" v-if="isedit">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['datamanager:gusmm:add']"
            >新增</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete()"
            v-hasPermi="['datamanager:gusmm:remove']"
            >删除</el-button
          >
        </el-col> -->
        <el-col :span="1.5" v-if="false">
          <el-button
            type="primary"
            plain
            icon="el-icon-download"
            size="mini"
            :disabled="multiple"
            @click="handleExport()"
            v-hasPermi="['datamanager:gusmm:export']"
            >导出</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-table
      :data="features"
      v-loading="loading"
      stripes
      highlight-current-row
      :height="tableHeight"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50"> </el-table-column>
      <el-table-column
        label="名称"
        align="center"
        :show-overflow-tooltip="true"
        prop="attributes.name"
      ></el-table-column>
      <el-table-column
        label="道路功能"
        width="80"
        align="center"
        :show-overflow-tooltip="true"
        prop="attributes.roadfunction"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_roadfunction"
            :value="scope.row.attributes.roadfunction"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="面积(㎡)"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ scope.row.attributes.Shape__Area.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-show="isedit"
            v-hasPermi="['datamanager:gusmm:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['datamanager:gusmm:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-location-outline"
            @click="handleLocate(scope.row)"
            >定位</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      layout="total, sizes,prev, pager, next"
      @pagination="getList"
    />
  </div>
</template>

<script>
import FeatureLayer from "@arcgis/core/layers/FeatureLayer";
import Graphic from "@arcgis/core/Graphic";
import SketchViewModel from "@arcgis/core/widgets/Sketch/SketchViewModel";
import GraphicsLayer from "@arcgis/core/layers/GraphicsLayer";
import Editor from "@arcgis/core/widgets/Editor";
import Sketch from "@arcgis/core/widgets/Sketch";
import axios from "axios";
import { mapGetters } from "vuex";
import { createLayer } from "../../../../utils/map";
export default {
  props: {
    mapview: { default: () => {} },
    roadfeature: { default: () => {} },
    roadcode: {
      type: String,
    },
    isedit: {
      type: Boolean,
    },
  },
  dicts: [
    "sys_roadclass_js",
    "sys_cleancompany",
    "sys_pavementmaterials",
    "sys_roadfunction",
  ],
  components: {},
  inject: ["toggleComponent"],
  computed: {
    theme() {
      return this.$store.state.settings.theme;
    },
  },
  data() {
    return {
      // 表格高度
      tableHeight: "65vh",
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 要素图层url
      featureLayerUrl:
        window.mapConfig.operationLayers.featurelayers["roadsurfacelayer"].url,
      // 要素图层
      featureLayer: undefined,
      // 图层视图
      layerView: undefined,
      highlight: undefined,
      // 当前页要素集合
      features: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 道路编码
      // roadcode:undefined
    };
  },
  created() {
    console.log("created");
  },
  mounted() {
    console.log("mounted");
    this.initFeatureLayer();
  },
  activated() {
    console.log("activated");
    console.log("this.featureLayer", this.featureLayer);
    this.initFeatureLayer();
  },
  deactivated() {
    let _self = this;
    console.log("deactivated");
    // 移除道路面图层
    _self.mapview.map.layers.remove(_self.featureLayer);
    _self.highlight?.remove();
    // _self.layerView = undefined;
    // _self.featureLayer = undefined;
  },
  methods: {
    /**
     * @description: 初始化featurelayer
     * @return {*}
     */
    initFeatureLayer() {
      let _self = this;
      console.log("_self.featureLayer", _self.featureLayer);

      _self.featureLayer = new FeatureLayer({
        title: "道路面",
        url: this.featureLayerUrl,
        outFields: ["objectid"],
        definitionExpression: `code='${_self.roadcode}'`,
      });
      console.log(
        "definitionExpression",
        _self.featureLayer.definitionExpression
      );
      _self.mapview.map.add(_self.featureLayer, 1);
      _self.mapview.whenLayerView(_self.featureLayer).then((layerView) => {
        console.log("roadsurface layerView loaded");
        _self.layerView = layerView;
        this.getList();
      });
    },

    /**
     * @description: 调用arcgis js api查询数据
     * @return {*}
     */
    getList() {
      let _self = this;
      _self.loading = true;
      // 先构建用于查询数据总条数的查询参数对象
      let queryParams = {
        // 输出字段
        outFields: ["objectid"],
        where: `code='${_self.roadcode}'`,
      };
      // 首先查询数据总条数用于渲染分页组件值
      _self.featureLayer.queryFeatureCount(queryParams).then((numResults) => {
        _self.total = numResults;
      });
      // 分页查询并在表格展示数据
      queryParams = {
        start: (_self.queryParams.pageNum - 1) * _self.queryParams.pageSize,
        num: _self.queryParams.pageSize,
        outFields: ["*"],
        returnGeometry: true,
        orderByFields: ["objectid"],
        where: `code='${_self.roadcode}'`,
      };
      // 查询数据
      _self.featureLayer.queryFeatures(queryParams).then((featureSet) => {
        _self.features = featureSet.features;
        _self.loading = false;
      });
    },
    /**
     * @description: 定位按钮操作
     * @param {*} row
     * @return {*}
     */
    async handleLocate(feature, isHighlight = true) {
      let _self = this;
      if (isHighlight) {
        // 移除前一个高亮图形
        _self.highlight?.remove();
        // 高亮显示当前页要素;
        _self.highlight = _self.layerView.highlight(feature);
      }
      if (feature.geometry == null) {
        _self.$message.error("暂无空间信息");
        return;
      }
      _self.mapview
        .goTo({
          target: feature.geometry,
          // zoom: _self.zoomLevel,
        })
        .catch((error) => {
          console.error(error);

          // if (error.name != "AbortError") {
          //   console.error(error);
          // }
        });
    },
    /**
     * @description: 多选框选中数据
     * @param {*} selection 选中的行
     * @return {*}
     */
    handleSelectionChange(selection) {
      // this.ids = selection.map((item) => item.attributes.objectid);
      this.selectFeatures = selection;
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      let _self = this;
      const paramObj = {
        featureLayer: _self.featureLayer,
        roadfeature: _self.roadfeature,
      };
      this.toggleComponent("roadsurfaceAddPanel", paramObj);
    },
    /**
     * @description: 修改按钮
     * @param {*} row 按钮所在数据行
     * @return {*}
     */
    handleUpdate(row) {
      let _self = this;
      _self.handleLocate(row, false);
      let paramObj = {
        feature: row,
        featureLayer: _self.featureLayer,
      };
      this.toggleComponent("roadsurfaceEditPanel", paramObj);
    },
    /**
     * @description: 删除按钮
     * @return {*}
     */
    handleDelete(row) {
      let _self = this;
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const deleteFeatures = [row];
          const edits = {
            deleteFeatures: deleteFeatures,
          };
          // 删除要素
          _self.featureLayer.applyEdits(edits).then((editsResult) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            console.log(editsResult);
            _self.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    cancel() {
      let _self = this;
      _self.mapview.map.layers.remove(_self.featureLayer);
      this.toggleComponent("roadDataPanel");
    },
    removeRoadSurfaceLayer() {
      let _self = this;
      _self.mapview.map.layers.remove(_self.featureLayer);
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  height: calc(100vh - 60px);
}

.map-container {
  padding: 0;
  margin: 0;
  width: 100%;
  height: calc(100vh - 60px);
}

.title-row {
  width: 100%;
  font-size: 18px;
  font-weight: bold;
  color: #409eff;
  margin-bottom: 1vh;
}

#idSplitInfoTitle {
  font-size: 18px !important;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: rgba(51, 51, 51, 1);
  padding: 0px 0px 0px 8px !important;
  //margin-left: 16px;
  border-left: 2px solid #008a96;
  margin-top: 10px;
  //position: absolute;
  z-index: 1;
}

.leftPanel {
  height: calc(100vh - 60px);
}

.rightPanel {
  height: calc(100vh - 60px);
  padding: 10px;

  .queryPanel {
    display: flex;
    flex-wrap: wrap;
  }
}
::v-deep .el-form-item {
  margin-bottom: 0px !important;
}

::v-deep .pagination-container {
  padding: 0px 16px !important;
  margin-top: 15px;
}
.chartPanel {
  width: 100%;
  height: calc(100vh - 60px);
}
</style>
